package com.ctg.platform.core.security.web;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.web.context.support.WebApplicationContextUtils;

import com.ctg.platform.core.SecurityConstants;
import com.ctg.platform.core.security.SpringSecurityManager;
/**
 * 
 * @author luli
 *
 */
public class ServletContextLoaderListener implements ServletContextListener {

	public void contextDestroyed(ServletContextEvent servletContextEvent) {
		servletContextEvent.getServletContext().removeAttribute(
		"urlAuthorities");
	}

	public void contextInitialized(ServletContextEvent servletContextEvent) {
		ServletContext servletContext = servletContextEvent.getServletContext();
		SpringSecurityManager springSecurityManager = this
				.getSpringSecurityManager(servletContext);
		//��Ҫ����ж��ٸ��Ʒ���࣬�ٸ�ݸ���Ʒ��װȨ���嵥
		List<String> productTypeList = springSecurityManager.loadProductTypeList();
		for (String productType : productTypeList) {
			Map<String, String> urlAuthorities = springSecurityManager.loadUrlAuthorities(productType);
			servletContext.setAttribute(SecurityConstants.AUTHORITIES+productType, urlAuthorities);
		}
	}

	protected SpringSecurityManager getSpringSecurityManager(ServletContext servletContext) {
		return (SpringSecurityManager) WebApplicationContextUtils
				.getWebApplicationContext(servletContext).getBean(
						"springSecurityManager");
	}
}
